﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using YJKGeometryFuncs;
using YJKCopyModel.QuickModelFrame;
using YJKPresentation.Utils;

namespace YJKCopyModel.DuctFastModel
{
	[Rebuilder(RebuilderName = "DuctFitRebuilder")]
	internal class DuctFitFastModelRebuilder : IRevitModelRebuilder
	{
		public bool TryRebuildRevitModel(Autodesk.Revit.DB.Document inputDoc, RevitModelRequest inputReqeust, out BuildData buildData)
		{
			buildData = new DuctBuildData();
			buildData.NeedResponse = true;
			bool flag = AllDuctData.DicBuildDatas.Count == 0;
			bool result;
			if (flag)
			{
				result = false;
			}
			else
			{
				List<XYZ> centerPoints = new List<XYZ>();
				foreach (KeyValuePair<int, DuctBuildData> data in AllDuctData.DicBuildDatas)
				{
					try
					{
						DuctBuildData value = data.Value;
						DuctInfo ductInfo = value.NewDuctFastModelRequest.DuctInfo;
						bool ismainPipe = ductInfo.ISMainPipe;
						if (ismainPipe)
						{
							Dictionary<int, int> dic = new Dictionary<int, int>();
							dic = ductInfo.GetLstConnectStart;
							this.CreateFit(inputDoc, dic, data, 0, ref centerPoints);
							dic = ductInfo.GetLstConnectEnd;
							this.CreateFit(inputDoc, dic, data, 1, ref centerPoints);
						}
					}
					catch (Exception ex)
					{
					}
				}
				bool bIsDrawCircle = DuctFormData.m_bIsDrawCircle;
				if (bIsDrawCircle)
				{
					buildData.UseWork = delegate
					{
						Transaction transaction = new Transaction(inputDoc, "未连接的风管末端画圈");
						try
						{
							transaction.Start();
							bool flag2 = !centerPoints.IsEmpty<XYZ>();
							if (flag2)
							{
								foreach (XYZ locationPt in centerPoints)
								{
									DuctDrawCricle.CreateCircle(inputDoc, locationPt);
								}
							}
							transaction.Commit();
						}
						catch (Exception ex)
						{
							bool flag3 = transaction.HasStarted();
							if (flag3)
							{
								transaction.RollBack();
							}
						}
					};
				}
				result = true;
			}
			return result;
		}

		private bool IsContainPt(List<XYZ> lstXyzs, XYZ ptXyz)
		{
			foreach (XYZ xyz in lstXyzs)
			{
				bool flag = ptXyz.IsAlmostEqualTo(xyz, Common.MMToFeet(600.0));
				if (flag)
				{
					return true;
				}
			}
			return false;
		}

		private void CreateFit(Autodesk.Revit.DB.Document inputDoc, Dictionary<int, int> dic, KeyValuePair<int, DuctBuildData> data, int nIndex, ref List<XYZ> centerPoints)
		{
			int count = dic.Count;
			DuctBuildData value = data.Value;
			DuctInfo ductInfo = value.NewDuctFastModelRequest.DuctInfo;
			bool flag = count > 0;
			if (flag)
			{
				List<KeyValuePair<DuctInfo, Connector>> list = new List<KeyValuePair<DuctInfo, Connector>>();
				Line line = ductInfo.ParaleCurve.LocationCurve as Line;
				list = this.GetConnectData(dic);
				Connector connectorAppoint = this.GetConnectorAppoint(data.Value.CreatedElement as Duct, ductInfo.ParaleCurve.LocationCurve.GetEndPoint(nIndex));
				bool flag2 = connectorAppoint != null;
				if (flag2)
				{
					double num = -1.0;
					bool bAppoint = DuctFormData.m_bAppoint;
					if (bAppoint)
					{
						num = DuctFormData.m_dDuctHeight;
					}
					Transaction transaction = new Transaction(inputDoc, "DuctFitFastModel");
					try
					{
						transaction.Start();
						FamilyInstance familyInstance = null;
						bool flag3 = count == 1;
						if (flag3)
						{
							Line line2 = list[0].Key.ParaleCurve.LocationCurve as Line;
							Connector value2 = list[0].Value;
							bool flag4 = YJKGeometryFuncs.Geometry.IsParallel(line.Direction, line2.Direction);
							if (flag4)
							{
								bool flag5 = !this.CreatUnion(inputDoc, connectorAppoint, value2, out familyInstance);
								if (flag5)
								{
									this.GetConnectorsCenter(connectorAppoint, value2, ref centerPoints);
								}
							}
							else
							{
								familyInstance = this.CreateElbow(inputDoc, connectorAppoint, value2);
								bool flag6 = familyInstance == null;
								if (flag6)
								{
									this.GetConnectorsCenter(connectorAppoint, value2, ref centerPoints);
								}
							}
						}
						else
						{
							bool flag7 = count == 2;
							if (flag7)
							{
								Line line3 = list[0].Key.ParaleCurve.LocationCurve as Line;
								Line line4 = list[1].Key.ParaleCurve.LocationCurve as Line;
								Connector value3 = list[0].Value;
								Connector value4 = list[1].Value;
								bool flag8 = YJKGeometryFuncs.Geometry.IsParallel(line.Direction, line3.Direction);
								if (flag8)
								{
									familyInstance = this.CreateTee(inputDoc, connectorAppoint, value3, value4);
								}
								else
								{
									bool flag9 = YJKGeometryFuncs.Geometry.IsParallel(line.Direction, line4.Direction);
									if (flag9)
									{
										familyInstance = this.CreateTee(inputDoc, connectorAppoint, value4, value3);
									}
									else
									{
										familyInstance = this.CreateTee(inputDoc, value3, value4, connectorAppoint);
									}
								}
								bool flag10 = familyInstance == null;
								if (flag10)
								{
									this.GetConnectorsCenter(connectorAppoint, value3, value4, ref centerPoints);
								}
							}
							else
							{
								bool flag11 = count == 3;
								if (flag11)
								{
									Line line5 = list[0].Key.ParaleCurve.LocationCurve as Line;
									Line line6 = list[1].Key.ParaleCurve.LocationCurve as Line;
									Line line7 = list[2].Key.ParaleCurve.LocationCurve as Line;
									Connector value5 = list[0].Value;
									Connector value6 = list[1].Value;
									Connector value7 = list[2].Value;
									bool flag12 = YJKGeometryFuncs.Geometry.IsParallel(line.Direction, line5.Direction);
									if (flag12)
									{
										bool flag13 = num == -1.0;
										if (flag13)
										{
											bool flag14 = ductInfo.Width * Convert.ToDouble(ductInfo.Height.dDuctHeight) >= list[0].Key.Width * Convert.ToDouble(list[0].Key.Height.dDuctHeight);
											if (flag14)
											{
												XYZ origin = connectorAppoint.Origin;
												XYZ origin2 = value5.Origin;
												XYZ origin3 = value6.Origin;
												bool flag15 = (origin.X - origin3.X) * (origin2.Y - origin3.Y) - (origin.Y - origin3.Y) * (origin2.X - origin3.X) > 0.0;
												bool flag16 = flag15;
												bool flag17 = flag16;
												if (flag17)
												{
													familyInstance = this.CreateCross(inputDoc, connectorAppoint, value5, value6, value7);
												}
												else
												{
													familyInstance = this.CreateCross(inputDoc, connectorAppoint, value5, value7, value6);
												}
											}
											else
											{
												XYZ origin4 = value5.Origin;
												XYZ origin5 = connectorAppoint.Origin;
												XYZ origin6 = value6.Origin;
												bool flag18 = (origin4.X - origin6.X) * (origin5.Y - origin6.Y) - (origin4.Y - origin6.Y) * (origin5.X - origin6.X) > 0.0;
												bool flag19 = flag18;
												bool flag20 = flag19;
												if (flag20)
												{
													familyInstance = this.CreateCross(inputDoc, value5, connectorAppoint, value6, value7);
												}
												else
												{
													familyInstance = this.CreateCross(inputDoc, value5, connectorAppoint, value7, value6);
												}
											}
										}
										else
										{
											bool flag21 = ductInfo.Width >= list[0].Key.Width;
											if (flag21)
											{
												XYZ origin7 = connectorAppoint.Origin;
												XYZ origin8 = value5.Origin;
												XYZ origin9 = value6.Origin;
												bool flag22 = (origin7.X - origin9.X) * (origin8.Y - origin9.Y) - (origin7.Y - origin9.Y) * (origin8.X - origin9.X) > 0.0;
												bool flag23 = flag22;
												bool flag24 = flag23;
												if (flag24)
												{
													familyInstance = this.CreateCross(inputDoc, connectorAppoint, value5, value6, value7);
												}
												else
												{
													familyInstance = this.CreateCross(inputDoc, connectorAppoint, value5, value7, value6);
												}
											}
											else
											{
												XYZ origin10 = value5.Origin;
												XYZ origin11 = connectorAppoint.Origin;
												XYZ origin12 = value6.Origin;
												bool flag25 = (origin10.X - origin12.X) * (origin11.Y - origin12.Y) - (origin10.Y - origin12.Y) * (origin11.X - origin12.X) > 0.0;
												bool flag26 = flag25;
												bool flag27 = flag26;
												if (flag27)
												{
													familyInstance = this.CreateCross(inputDoc, value5, connectorAppoint, value6, value7);
												}
												else
												{
													familyInstance = this.CreateCross(inputDoc, value5, connectorAppoint, value7, value6);
												}
											}
										}
									}
									else
									{
										bool flag28 = YJKGeometryFuncs.Geometry.IsParallel(line.Direction, line6.Direction);
										if (flag28)
										{
											bool flag29 = num == -1.0;
											if (flag29)
											{
												bool flag30 = ductInfo.Width * Convert.ToDouble(ductInfo.Height.dDuctHeight) >= list[1].Key.Width * Convert.ToDouble(list[1].Key.Height.dDuctHeight);
												if (flag30)
												{
													XYZ origin13 = connectorAppoint.Origin;
													XYZ origin14 = value6.Origin;
													XYZ origin15 = value5.Origin;
													bool flag31 = (origin13.X - origin15.X) * (origin14.Y - origin15.Y) - (origin13.Y - origin15.Y) * (origin14.X - origin15.X) > 0.0;
													bool flag32 = flag31;
													bool flag33 = flag32;
													if (flag33)
													{
														familyInstance = this.CreateCross(inputDoc, connectorAppoint, value6, value5, value7);
													}
													else
													{
														familyInstance = this.CreateCross(inputDoc, connectorAppoint, value6, value7, value5);
													}
												}
												else
												{
													XYZ origin16 = value6.Origin;
													XYZ origin17 = connectorAppoint.Origin;
													XYZ origin18 = value5.Origin;
													bool flag34 = (origin16.X - origin18.X) * (origin17.Y - origin18.Y) - (origin16.Y - origin18.Y) * (origin17.X - origin18.X) > 0.0;
													bool flag35 = flag34;
													bool flag36 = flag35;
													if (flag36)
													{
														familyInstance = this.CreateCross(inputDoc, value6, connectorAppoint, value5, value7);
													}
													else
													{
														familyInstance = this.CreateCross(inputDoc, value6, connectorAppoint, value7, value5);
													}
												}
											}
											else
											{
												bool flag37 = ductInfo.Width >= list[1].Key.Width;
												if (flag37)
												{
													XYZ origin19 = connectorAppoint.Origin;
													XYZ origin20 = value6.Origin;
													XYZ origin21 = value5.Origin;
													bool flag38 = (origin19.X - origin21.X) * (origin20.Y - origin21.Y) - (origin19.Y - origin21.Y) * (origin20.X - origin21.X) > 0.0;
													bool flag39 = flag38;
													bool flag40 = flag39;
													if (flag40)
													{
														familyInstance = this.CreateCross(inputDoc, connectorAppoint, value6, value5, value7);
													}
													else
													{
														familyInstance = this.CreateCross(inputDoc, connectorAppoint, value6, value7, value5);
													}
												}
												else
												{
													XYZ origin22 = value6.Origin;
													XYZ origin23 = connectorAppoint.Origin;
													XYZ origin24 = value5.Origin;
													bool flag41 = (origin22.X - origin24.X) * (origin23.Y - origin24.Y) - (origin22.Y - origin24.Y) * (origin23.X - origin24.X) > 0.0;
													bool flag42 = flag41;
													bool flag43 = flag42;
													if (flag43)
													{
														familyInstance = this.CreateCross(inputDoc, value6, connectorAppoint, value5, value7);
													}
													else
													{
														familyInstance = this.CreateCross(inputDoc, value6, connectorAppoint, value7, value5);
													}
												}
											}
										}
										else
										{
											bool flag44 = YJKGeometryFuncs.Geometry.IsParallel(line.Direction, line7.Direction);
											if (flag44)
											{
												bool flag45 = num == -1.0;
												if (flag45)
												{
													bool flag46 = ductInfo.Width * Convert.ToDouble(ductInfo.Height.dDuctHeight) >= list[2].Key.Width * Convert.ToDouble(list[2].Key.Height.dDuctHeight);
													if (flag46)
													{
														XYZ origin25 = connectorAppoint.Origin;
														XYZ origin26 = value7.Origin;
														XYZ origin27 = value5.Origin;
														bool flag47 = (origin25.X - origin27.X) * (origin26.Y - origin27.Y) - (origin25.Y - origin27.Y) * (origin26.X - origin27.X) > 0.0;
														bool flag48 = flag47;
														bool flag49 = flag48;
														if (flag49)
														{
															familyInstance = this.CreateCross(inputDoc, connectorAppoint, value7, value5, value6);
														}
														else
														{
															familyInstance = this.CreateCross(inputDoc, connectorAppoint, value7, value6, value5);
														}
													}
													else
													{
														XYZ origin28 = value7.Origin;
														XYZ origin29 = connectorAppoint.Origin;
														XYZ origin30 = value5.Origin;
														bool flag50 = (origin28.X - origin30.X) * (origin29.Y - origin30.Y) - (origin28.Y - origin30.Y) * (origin29.X - origin30.X) > 0.0;
														bool flag51 = flag50;
														bool flag52 = flag51;
														if (flag52)
														{
															familyInstance = this.CreateCross(inputDoc, value7, connectorAppoint, value5, value6);
														}
														else
														{
															familyInstance = this.CreateCross(inputDoc, value7, connectorAppoint, value6, value5);
														}
													}
												}
												else
												{
													bool flag53 = ductInfo.Width >= list[2].Key.Width;
													if (flag53)
													{
														XYZ origin31 = connectorAppoint.Origin;
														XYZ origin32 = value7.Origin;
														XYZ origin33 = value5.Origin;
														bool flag54 = (origin31.X - origin33.X) * (origin32.Y - origin33.Y) - (origin31.Y - origin33.Y) * (origin32.X - origin33.X) > 0.0;
														bool flag55 = flag54;
														bool flag56 = flag55;
														if (flag56)
														{
															familyInstance = this.CreateCross(inputDoc, connectorAppoint, value7, value5, value6);
														}
														else
														{
															familyInstance = this.CreateCross(inputDoc, connectorAppoint, value7, value6, value5);
														}
													}
													else
													{
														XYZ origin34 = value7.Origin;
														XYZ origin35 = connectorAppoint.Origin;
														XYZ origin36 = value5.Origin;
														bool flag57 = (origin34.X - origin36.X) * (origin35.Y - origin36.Y) - (origin34.Y - origin36.Y) * (origin35.X - origin36.X) > 0.0;
														bool flag58 = flag57;
														bool flag59 = flag58;
														if (flag59)
														{
															familyInstance = this.CreateCross(inputDoc, value7, connectorAppoint, value5, value6);
														}
														else
														{
															familyInstance = this.CreateCross(inputDoc, value7, connectorAppoint, value6, value5);
														}
													}
												}
											}
										}
									}
									bool flag60 = familyInstance == null;
									if (flag60)
									{
										this.GetConnectorsCenter(connectorAppoint, value5, value6, value7, ref centerPoints);
									}
								}
							}
						}
						transaction.Commit();
					}
					catch (Exception ex)
					{
						bool flag61 = transaction.HasStarted();
						if (flag61)
						{
							transaction.RollBack();
						}
					}
				}
				foreach (KeyValuePair<int, int> keyValuePair in dic)
				{
					DuctBuildData ductBuildData;
					AllDuctData.DicBuildDatas.TryGetValue(keyValuePair.Key, out ductBuildData);
					DuctInfo ductInfo2 = ductBuildData.NewDuctFastModelRequest.DuctInfo;
					Dictionary<int, int> dic2 = new Dictionary<int, int>();
					int nIndex2 = 0;
					bool flag62 = keyValuePair.Value == 0;
					if (flag62)
					{
						dic2 = ductInfo2.GetLstConnectEnd;
						nIndex2 = 1;
					}
					else
					{
						dic2 = ductInfo2.GetLstConnectStart;
					}
					KeyValuePair<int, DuctBuildData> data2 = new KeyValuePair<int, DuctBuildData>(keyValuePair.Key, ductBuildData);
					this.CreateFit(inputDoc, dic2, data2, nIndex2, ref centerPoints);
				}
			}
		}

		private void GetConnectorsCenter(Connector c1, Connector c2, ref List<XYZ> centers)
		{
			bool flag = c1 == null || c2 == null;
			if (!flag)
			{
				XYZ xyz = (c1.Origin + c2.Origin) / 2.0;
				bool flag2 = !this.IsContainPt(centers, xyz);
				if (flag2)
				{
					centers.Add(xyz);
				}
			}
		}

		private void GetConnectorsCenter(Connector c1, Connector c2, Connector c3, ref List<XYZ> centers)
		{
			bool flag = c1 == null || c2 == null || c3 == null;
			if (!flag)
			{
				XYZ xyz = (c1.Origin + c2.Origin) / 2.0;
				xyz = (xyz + c3.Origin) / 2.0;
				bool flag2 = !this.IsContainPt(centers, xyz);
				if (flag2)
				{
					centers.Add(xyz);
				}
			}
		}

		private void GetConnectorsCenter(Connector c1, Connector c2, Connector c3, Connector c4, ref List<XYZ> centers)
		{
			bool flag = c1 == null || c2 == null || c3 == null || c4 == null;
			if (!flag)
			{
				XYZ xyz = (c1.Origin + c2.Origin) / 2.0;
				XYZ xyz2 = (c3.Origin + c4.Origin) / 2.0;
				XYZ xyz3 = (xyz + xyz2) / 2.0;
				bool flag2 = !this.IsContainPt(centers, xyz3);
				if (flag2)
				{
					centers.Add(xyz3);
				}
			}
		}

		private Connector GetConnectorAppoint(Duct duct, XYZ pt)
		{
			Connector result;
			try
			{
				Connector connector = null;
				foreach (object obj in duct.ConnectorManager.Connectors)
				{
					Connector connector2 = (Connector)obj;
					XYZ xyz = new XYZ(connector2.Origin.X, connector2.Origin.Y, 0.0);
					bool flag = xyz.IsAlmostEqualTo(pt);
					if (flag)
					{
						connector = connector2;
					}
				}
				result = connector;
			}
			catch (Exception)
			{
				result = null;
			}
			return result;
		}

		private List<KeyValuePair<DuctInfo, Connector>> GetConnectData(Dictionary<int, int> lstDic)
		{
			List<KeyValuePair<DuctInfo, Connector>> list = new List<KeyValuePair<DuctInfo, Connector>>();
			foreach (KeyValuePair<int, int> keyValuePair in lstDic)
			{
				DuctBuildData ductBuildData = new DuctBuildData();
				AllDuctData.DicBuildDatas.TryGetValue(keyValuePair.Key, out ductBuildData);
				Duct duct = ductBuildData.CreatedElement as Duct;
				Curve locationCurve = ductBuildData.NewDuctFastModelRequest.DuctInfo.ParaleCurve.LocationCurve;
				XYZ endPoint = locationCurve.GetEndPoint(keyValuePair.Value);
				Connector value = null;
				foreach (object obj in duct.ConnectorManager.Connectors)
				{
					Connector connector = (Connector)obj;
					XYZ xyz = new XYZ(connector.Origin.X, connector.Origin.Y, 0.0);
					bool flag = xyz.IsAlmostEqualTo(endPoint);
					if (flag)
					{
						value = connector;
					}
				}
				list.Add(new KeyValuePair<DuctInfo, Connector>(ductBuildData.NewDuctFastModelRequest.DuctInfo, value));
			}
			return list;
		}

		private bool CreatUnion(Autodesk.Revit.DB.Document inputDoc, Connector one, Connector other, out FamilyInstance createdFi)
		{
			createdFi = null;
			bool result;
			try
			{
				createdFi = inputDoc.Create.NewUnionFitting(one, other);
				result = true;
			}
			catch
			{
				result = false;
			}
			return result;
		}

		private FamilyInstance CreateElbow(Autodesk.Revit.DB.Document inputDoc, Connector one, Connector other)
		{
			FamilyInstance result;
			try
			{
				result = inputDoc.Create.NewElbowFitting(one, other);
			}
			catch
			{
				result = null;
			}
			return result;
		}

		private FamilyInstance CreateTee(Autodesk.Revit.DB.Document inputDoc, Connector one, Connector two, Connector three)
		{
			FamilyInstance result;
			try
			{
				result = inputDoc.Create.NewTeeFitting(one, two, three);
			}
			catch
			{
				result = null;
			}
			return result;
		}

		private FamilyInstance CreateCross(Autodesk.Revit.DB.Document inputDoc, Connector one, Connector two, Connector three, Connector four)
		{
			FamilyInstance result;
			try
			{
				result = inputDoc.Create.NewCrossFitting(one, two, three, four);
			}
			catch
			{
				result = null;
			}
			return result;
		}
	}
}
